package com.bocloud.cmp.web.controller.resource;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.common.enums.BoCloudMethod;
import com.bocloud.common.enums.BoCloudService;
import com.bocloud.common.model.BsmResult;
import com.bocloud.common.utils.Common;
import com.bocloud.common.utils.MapTools;
import com.bocloud.common.utils.ResultTools;
import com.bocloud.registry.http.core.ServiceFactory;
import com.bocloud.registry.http.model.RemoteService;
import com.bocloud.registry.utils.UrlTranslator;

/**
 * 资源关联关系入口
 * 
 * @author dongkai
 *
 */
@RestController
@RequestMapping("/res/relation")
public class ResourceRelationController {

	private final String BASE_SERVICE = "/res/relation";
	private static final BoCloudService SERVICE = BoCloudService.Cmp;
	@Autowired
	private ServiceFactory serviceFactory;

	/**
	 * 获取目标节点
	 * 
	 * @param params
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/targets", method = { RequestMethod.POST })
	public BsmResult targets(@RequestParam(value = Common.PARAMS, required = true) String params,
			HttpServletRequest request) {
		JSONObject jsonObject = JSONObject.parseObject(params);
		if (null != jsonObject && jsonObject.containsKey(Common.ID)) {
			Map<String, Object> paramMap = MapTools.simpleMap("category", jsonObject.getString("category"));
			String url = UrlTranslator.translate(BASE_SERVICE + "/targets", jsonObject.getLong(Common.ID));
			RemoteService service = serviceFactory.safeBuild(SERVICE, url, BoCloudMethod.OTHERS, null, paramMap,
					request);
			return service.invoke();
		} else {
			return ResultTools.formatErrResult();
		}
	}

	/**
	 * 查询所有
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/listAll", method = { RequestMethod.POST })
	public BsmResult listAll(HttpServletRequest request) {
		RemoteService service = serviceFactory.safeBuild(SERVICE, BASE_SERVICE + "/listAll", BoCloudMethod.OTHERS, null,
				null, request);
		return service.invoke();
	}

}
